package com.ee.bruscar.configuration;

import java.util.List;

import com.ee.bruscar.configuration.hint.Hint;
import com.ee.bruscar.configuration.hint.MethodHint;
import com.ee.bruscar.configuration.hint.ModelHint;
import com.ee.bruscar.configuration.hint.lookup.HintLookup;
import com.ee.bruscar.exceptions.BruscarException;

public class InterfaceConfiguration implements HintLookup
{
	private String interfaceToHonour;
	private String stubClass;
	private String srcPath;
	
	private List<MethodHint> methodHints;
	private List<ModelHint> modelHints;

	public List<ModelHint> getModelHints()
	{
		return modelHints;
	}

	public void setModelHints(List<ModelHint> modelHints)
	{
		this.modelHints = modelHints;
	}

	public InterfaceConfiguration(String interfaceName, String outputClassName, String srcPath )
	{
		this.interfaceToHonour = interfaceName;
		this.stubClass = outputClassName;
		this.srcPath = srcPath;
	}

	public List<MethodHint> getMethodHints()
	{
		return methodHints;
	}

	public void setMethodHints(List<MethodHint> methodHints)
	{
		this.methodHints = methodHints;
	}
	
	public void setInterfaceToHonour(String interfaceToHonour)
	{
		this.interfaceToHonour = interfaceToHonour;
	}

	public void setStubClass(String stubClass)
	{
		this.stubClass = stubClass;
	}

	public void setSrcPath(String srcPath)
	{
		this.srcPath = srcPath;
	}

	public String getSrcPath()
	{
		return srcPath;
	}

	public String getStubClass()
	{
		return stubClass;
	}

	public String getInterfaceToHonour()
	{
		return interfaceToHonour;
	}
	
	public Class<?> getInterfaceToHonourClass()
	{
		try
		{
			return Class.forName( interfaceToHonour );
		} 
		catch (ClassNotFoundException e)
		{
			throw new RuntimeException("Can't load a class for: " + interfaceToHonour );
		}
	}
	

	public String getHint(String className, String setterName)
	{
		return null;
	}
	
	public Hint getHintFor(String methodName)
	{
		for( MethodHint hint : methodHints )
		{
			if( hint.getMethodName().equals( methodName ))
			{
				if( hint.getModelHint() != null )
				{
					ModelHint modelHint = hint.getModelHint();
					
					if( modelHint.getRef() != null )
					{
						ModelHint referencedHint = getModelHintFor( modelHint.getRef() );
						//TODO I'm manually forward on the range value here, this could be done more elegantly.
						referencedHint.setRange( modelHint.getRange() );
						return referencedHint;
					}
					
					return hint.getModelHint();
				}
				return hint;
			}
		}
		return null;
	}

	public ModelHint getModelHintFor(String ref)
	{
		for( ModelHint hint : modelHints )
		{
			if( hint.getId().equals( ref ))
			{
				return hint;
			}
		}
		throw new BruscarException("can't find referenced hint: " + ref);
	}
}
